let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLoading: true,
        keyword: '',  //搜索文本框
        isShowScreen: false,  //是否显示筛选框
        progressCode: 0,  //筛选 - 进展状态
        commentCode: 0, //筛选 - 是否点评
        projectCode: 0, //筛选 - 项目类别
        projectMyname: 0,
        statusCode:0,
        page: 1,  //页码
        limit: 10,
        scrollTop: 0,  //初始化滚动距离
        projectList: [],
        isTxt: false,
        updatevalue:'',
        libraryTab:0,
        sign:0,
        start_status:0,
        warehousing:0,
        reply:0,
        operation:0,
        enterprise:0,
        cultivation:0,
        condition_val:0,
        invest_type:0
    },


    onMenuTab:function(e){
        this.setData({libraryTab:e.currentTarget.dataset.by});
        this.getProject();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.init();
    },

    async init(){
        //获取筛选条件
        await app.request('post', '/api/Project/screenApi', {appid: app.globalData.appid})
        .then(res => {
            this.setData({ progress: res.data.progress, comment: res.data.comment, projectType: res.data.type });
        }).catch(err => console.log(err));
        if(wx.getStorageSync('projectTypeName')){
            if(!Array.isArray(this.data.projectType)) return false;
            this.data.projectType.forEach(item => {
                if(item.title == wx.getStorageSync('projectTypeName')) this.setData({ projectCode: item.value });
            });
        }
        this.getProject();
        wx.removeStorageSync('projectTypeName');

    },

    /** 搜索框输入 */
    inputSearch(e){
        this.setData({ keyword: e.detail.value, isTxt: true});
    },

    emptySearch(){
        this.setData({ keyword: '', isTxt: false});
    },

    /** 打开筛选 */
    onShowScreen(){
        this.setData({ isShowScreen: true });
    },

    /** 关闭筛选 */
    onCloseScreen(){
        this.setData({ isShowScreen: false });
    },

    /** 筛选 - 进展状态 */
    onChangeProgress(e){
        this.setData({ progressCode: e.currentTarget.dataset.value });
    },

    /** 筛选 - 是否点评 */
    onChangeComment(e){
        this.setData({ commentCode: e.currentTarget.dataset.value });
    },

    /** 筛选 - 项目类别 */
    onChangeProject(e){
        this.setData({ projectCode: e.currentTarget.dataset.value });
    },
    
    /** 筛选 - 重置 */
    onReset(){
        this.setData({ progressCode: 0, commentCode: 0, projectCode: 0 });
    },

    /** 筛选 - 确定 */
    onSubmit(){
        this.setData({ isShowScreen: false, page: 1, scrollTop: 0, projectList: [], isLoading: true });
        this.getProject();
    },

    onSearchBy(e){
        let key = e.currentTarget.dataset.by;
        //全部
        if(key == 'all'){
          this.setData({ progressCode: 0, commentCode: 0, projectCode: 0, page: 1, scrollTop: 0, projectList: [], isLoading: true, projectMyname: 0 ,statusCode:0});
            this.getProject();
        }
        //未完成
        if(key == 'over'){
            this.setData({ progressCode: 2, commentCode: 0, projectCode: 0, page: 1, scrollTop: 0, projectList: [], isLoading: true ,statusCode:0});
            this.getProject();
        }
        //正常
        if(key == 'normal'){
            this.setData({ progressCode: 1, commentCode: 0, projectCode: 0, page: 1, scrollTop: 0, projectList: [], isLoading: true ,statusCode:0});
            this.getProject();
        }
        //我的
        if(key == 'assess'){
            this.setData({ progressCode: 0, projectMyname: 1, projectCode: 0, page: 1, scrollTop: 0, projectList: [], isLoading: true ,statusCode:0});
            this.getProject();
        }
        //逾期
        if(key == 'status'){
            this.setData({ progressCode: 0, commentCode: 0, projectCode: 0, page: 1, scrollTop: 0, projectList: [], isLoading: true, projectMyname: 0 ,statusCode:2});
            this.getProject();
        }

        let value = e.currentTarget.dataset.value;
        //签约
        if(key == 'sign'){
            this.setData({ sign:value});
            this.getProject();
        }

        //开工
        if(key == 'start_status'){
            this.setData({ start_status:value});
            this.getProject();
        }

        //入库
        if(key == 'warehousing'){
            this.setData({ warehousing:value});
            this.getProject();
        }

        //启报
        if(key == 'reply'){
            this.setData({ reply:value});
            this.getProject();
        }

         //启报
         if(key == 'operation'){
            this.setData({ operation:value});
            this.getProject();
        }
    },

    /** 获取项目列表 */
    getProject(){
        let params = {
            complete: this.data.progressCode,
            comment: this.data.commentCode,
            project_type: this.data.projectCode,
            keyword: this.data.keyword,
            myname: this.data.projectMyname,
            project_status: this.data.statusCode,
            page: 1,
            limit: this.data.page * this.data.limit,
            library:this.data.libraryTab,
            sign:this.data.sign,
            start_status:this.data.start_status,
            warehousing:this.data.warehousing,
            reply:this.data.reply,
            operation:this.data.operation,
            cultivation:this.data.cultivation,
            enterprise:this.data.enterprise,
            condition_val:this.data.condition_val,
            invest_type:this.data.invest_type
        };



        //获取项目列表
        app.request('post', '/api/Project/listApi', params).then(res => {
            let projectList = res.data.list;
            projectList.forEach(item => {
                if(item.difficulty.length > 30) item.difficulty = `${item.difficulty.substr(0, 32)}...`;
            });
            this.setData({ projectList, projectTotal: res.data.count, isLoading: false });
        }).catch(err => console.log(err));
    },

    /** 滚动触底 */
    scrollBottom(e){
        let page = this.data.page;
        if(this.data.projectList.length >= 10){
            if(page * this.data.limit < this.data.projectTotal){
                page += 1;
                this.setData({ page });
                this.getProject();
            }else{
                app.showFailToast('已经到底了');
            }
        }else{
            app.showFailToast('没有更多数据了');
        }
    },

    /** 点评 */
    onAssess(e){
        wx.setStorageSync('projectInfo', e.currentTarget.dataset.item);
        wx.navigateTo({ url: `/pages/assess/assess` });
    },

    /** 点击进入详情页 */
    onClickDetail(e){
        var libraryTab = this.data.libraryTab;
        wx.navigateTo({ url: `/pages/project/project?id=${e.currentTarget.dataset.id}&libraryTab=${libraryTab}` })
    },

    //推进困难弹窗
    onClickDetail1(e){
        this.setData({
            updatevalue:e.currentTarget.dataset.id
        });
        this.popup.showPopup();
    },

    onProgress(e){
        let id = e.currentTarget.dataset.id;
        let sign = e.currentTarget.dataset.sign;
        let key = e.currentTarget.dataset.key;
        let index = e.currentTarget.dataset.index;
        let params = {
            id: id,
            schedule: key == 'pro' ? 'progress' : 'year_progress',
            operation: sign == 'plus' ? 1 : 2,
            number: 1
        };


    
        if(key=='pro'){
            if(sign=='plus'){
                if(Number(this.data.projectList[index].progress)+1<=100){
                    this.data.projectList[index].progress =Number(this.data.projectList[index].progress)+1
                }
            }else{
                if(Number(this.data.projectList[index].progress)-1>=0){
                    this.data.projectList[index].progress =Number(this.data.projectList[index].progress)-1
                }
            }
            
        }else{
            if(sign=='plus'){
                if(Number(this.data.projectList[index].year_progress)+1<=100){
                    this.data.projectList[index].year_progress=Number(this.data.projectList[index].year_progress)+1
                }
            }else{
                if(Number(this.data.projectList[index].year_progress)-1>=0){
                    this.data.projectList[index].year_progress=Number(this.data.projectList[index].year_progress)-1
                }
            }
        }
       
        app.request('post', '/api/Project/schedule', params).then(res => {
            console.log(res);
            this.setData({projectList:this.data.projectList});
        }).catch(err => console.log(err));
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        if(options.libraryTab!=undefined){
            this.setData({libraryTab:options.libraryTab});
        }

        //签约状态
        if(options.sign!=undefined){
          this.setData({sign:options.sign});
        }

        //开工状态
        if(options.start_status!=undefined){
            this.setData({start_status:options.start_status});
        }

        //项目状态
        if(options.statusCode!=undefined){
            this.setData({statusCode:options.statusCode});
        }

        //我的项目
        if(options.projectMyname!=undefined){
            this.setData({projectMyname:options.projectMyname});
        }

        //启报状态
        if(options.reply!=undefined){
            this.setData({reply:options.reply});
        }
        
        //培育类相关
        if(options.enterprise!=undefined){
            this.setData({enterprise:options.enterprise});
        }

        if(options.cultivation!=undefined){
            this.setData({cultivation:options.cultivation});
        }

        if(options.condition_val!=undefined){
            this.setData({condition_val:options.condition_val});
        }
        
        if(options.projectCode!=undefined){
            this.setData({projectCode:options.projectCode});
        }

        if(options.warehousing!=undefined){
            this.setData({warehousing:options.warehousing});
        }

        if(options.invest_type!=undefined){
            this.setData({invest_type:options.invest_type});
        }
        
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.popup = this.selectComponent('#popup');
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    //取消事件
    _error() {
        this.popup.hidePopup();
    },

    //打开监控页面
    live(e){
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({ url: `/pages/live/live?id=`+id})
        // var url = e.currentTarget.dataset.url;        
        // wx.navigateTo({ url: `/pages/live/live?url=`+ encodeURIComponent(url) })
    },
})